import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import '../../controller/event/event_declration.dart';
import '../../controller/event/loading_event.dart';
import '../../utils/router/log_util.dart';

StreamSubscription initLoadingDialog(BuildContext? context) {
  LogUtil.get().w('初始化监听------------------------Loading---------------------');

  StreamSubscription _streamLoading =
      eventBus.on<LoadingEvent>().listen((event) {
    try {
      ///当前没有加载中展示,且展示标识为否的时候
      if (event.showFlag) {
        // LogUtil.get().d('准备显示Loading信息');

        SmartDialog.show(
          builder: (context) => LoadingDialog(),
          tag: 'loadingDialog',
          clickMaskDismiss: false,
          useAnimation: false,
          animationType: SmartAnimationType.scale,
          onDismiss: () {
            print('dialog dismiss');
          },
        );
      } else {
        // LogUtil.get().d('准备2秒后关闭Loading信息');
        Future.delayed(Duration(seconds: 1),
            () => SmartDialog.dismiss(tag: 'loadingDialog'));
      }
    } catch (e) {
      LogUtil.get().e('准备关闭Loading error = $e');
    }
  });
  return _streamLoading;
}

class LoadingDialog extends Dialog {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: SizedBox(
        width: 120,
        height: 60,
        child: Opacity(
          opacity: 1.0,
          child: Container(
            ///弹框背景和圆角
            decoration: ShapeDecoration(
              color: Colors.transparent,
              image: DecorationImage(
                image: ExactAssetImage('assets/images/ZKI-loading.gif'),
              ),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(
                  Radius.circular(8.0),
                ),
              ),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(
                    top: 20.0,
                  ),
                  // child:  Text(text),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
